package com.xxcxcxcx.jobsystem.common.Job.Brewmaster;

import com.xxcxcxcx.jobsystem.Main;
import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.Job.JobBase;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapability;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.LazyOptional;

import java.util.Random;

public class Brewmaster extends JobBase {
    public static String Job= JobsClass.Brewmaster;
    public static String Refining= "refining";
    public static String RefiningCreater= "creater";
    public static String RefiningLevel= "level";
    public static String Refiningtime= "time";
    public static String RefiningKind= "kind";
    public static String[] RefiningKinds= {"enhance","extended","preenhance","preextended","perfect"};
    public static boolean hasPotionBonus(ItemStack itemStack){
        CompoundTag n=itemStack.getOrCreateTag();
        return hasPotionBonus(n);
    }
    public static boolean hasPotionBonus(CompoundTag n){
        if(n.contains(Main.MOD_ID)){
            n=n.getCompound(Main.MOD_ID);
            if(n.contains(Refining)){
                return true;
            }
        }
        return false;
    }
    public static void addPotionBonus(ItemStack itemStack, Player player) {
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        int JobLevel = playerSaveData.getLevel(Job);
        Random random = player.getRandom();
        if(JobLevel==0){
            double r=player.getRandom().nextDouble();
            if(r<0.9){
                itemStack.setCount(0);
            }else if(random.nextBoolean()){
                Refiningelixir(itemStack,player,0,itemStack.getCount());
            }else{
                Refiningelixir(itemStack,player,1,itemStack.getCount());
            }
        }else if(JobLevel<4){
            double r=player.getRandom().nextDouble();
            if(r<0.9-JobLevel*0.2){
                itemStack.setCount(0);
            }else if(random.nextBoolean()){
                Refiningelixir(itemStack,player,0,itemStack.getCount());
            }else{
                Refiningelixir(itemStack,player,1,itemStack.getCount());
            }
        }else if(JobLevel<7){
            double r=player.getRandom().nextDouble();
            if(r<0.1){
                itemStack.setCount(0);
            }else if(r<0.2+(JobLevel-4)*0.05){
                if(random.nextBoolean()){
                    Refiningelixir(itemStack,player,3,itemStack.getCount());
                }else{
                    Refiningelixir(itemStack,player,2,itemStack.getCount());
                }
            }
            else if(random.nextBoolean()){
                Refiningelixir(itemStack,player,0,itemStack.getCount());
            }else{
                Refiningelixir(itemStack,player,1,itemStack.getCount());
            }
        }else if(JobLevel<10){
            double r=player.getRandom().nextDouble();
            if(r<0.2+(JobLevel-4)*0.05){
                if(random.nextBoolean()){
                    Refiningelixir(itemStack,player,3,itemStack.getCount());
                }else{
                    Refiningelixir(itemStack,player,2,itemStack.getCount());
                }
            }
            else if(random.nextBoolean()){
                Refiningelixir(itemStack,player,0,itemStack.getCount());
            }else{
                Refiningelixir(itemStack,player,1,itemStack.getCount());
            }
        }else{
            double r=player.getRandom().nextDouble();
            if(r<0.01){
                Refiningelixir(itemStack,player,4,itemStack.getCount());
            }else if(r<0.2+(JobLevel-4)*0.05){
                if(random.nextBoolean()){
                    Refiningelixir(itemStack,player,3,itemStack.getCount());
                }else{
                    Refiningelixir(itemStack,player,2,itemStack.getCount());
                }
            }
            else if(random.nextBoolean()){
                Refiningelixir(itemStack,player,0,itemStack.getCount());
            }else{
                Refiningelixir(itemStack,player,1,itemStack.getCount());
            }
        }
        if(itemStack.isEmpty()){
            player.playSound(SoundEvents.GLASS_BREAK,1.0f,1.0f);
            UpExperience(player,10, Job);
        }
    }
    public static ItemStack addPotionBonus(ItemStack itemStack, String player,int GLevel,int level,float time) {
        CompoundTag nbt=itemStack.getOrCreateTagElement(Main.MOD_ID);
        nbt.putInt(Refining,GLevel);
        nbt.putString(Refining+RefiningCreater,player);
        nbt.putInt(Refining+RefiningLevel,level);
        nbt.putFloat(Refining+Refiningtime,time);
        return itemStack;
    }
    public static void Refiningelixir(ItemStack itemStack,Player player,int GLevel,int num){
        UpExperience(player,(GLevel+1+(GLevel+1)%2)*20*num, Job);
        CompoundTag nbt=itemStack.getOrCreateTagElement(Main.MOD_ID);
        nbt.putInt(Refining,GLevel);
        nbt.putString(Refining+RefiningCreater,player.getName().getString());
        Random random = player.getRandom();
        switch (GLevel) {
            case 0 -> {
                nbt.putInt(Refining + RefiningLevel, 1);
                nbt.putFloat(Refining + Refiningtime, random.nextFloat(0.1f, 0.5f));
            }
            case 1 -> {
                nbt.putInt(Refining + RefiningLevel, -1);
                nbt.putFloat(Refining + Refiningtime, random.nextFloat(1.5f, 2.0f));
            }
            case 2 -> {
                nbt.putInt(Refining + RefiningLevel, 2);
                nbt.putFloat(Refining + Refiningtime, random.nextFloat(0.9f, 1.0f));
            }
            case 3 -> nbt.putFloat(Refining + Refiningtime, random.nextFloat(2.5f, 4.0f));
            case 4 -> {
                nbt.putInt(Refining + RefiningLevel, 4);
                nbt.putFloat(Refining + Refiningtime, random.nextFloat(4.0f, 6.0f));
            }
        }
    }

    public static MobEffectInstance applyPotionBonus(MobEffectInstance mobEffectInstance,int level,float time) {
        boolean instant = mobEffectInstance.getEffect().isInstantenous();
        return new MobEffectInstance(
                mobEffectInstance.getEffect(),
                Math.round(mobEffectInstance.getDuration()  * time),
                Math.max(0,mobEffectInstance.getAmplifier() + level),
                mobEffectInstance.isAmbient(),
                mobEffectInstance.isVisible(),
                mobEffectInstance.showIcon()
        );
    }
}
